%%
%% cas login controller
%% 
-module(ecas_controller,[Env]).

-export([handle_request/2,before_filter/0]).

handle_request("login",[]) ->
%%    ecas_db:reset_tables(),    
 ServiceUrl  = uri_params_middleware:get_param("service",Env),
                      Url=if
			      ServiceUrl=:=undefined ->
				  "";
			      true->ServiceUrl
                           end ,
 Cookies = ewgi_api:get_header_value("cookie", Env),
        Rtn = case Cookies of
                undefined -> {render,"ecas/login.html",[{service,Url},{message,[]}],[]};
                 _ ->
		      Cookie = proplists:get_value("CASTGT", beepbeep_cookies:parse_cookie(Cookies)),
                      io:format("cas tgt:~p~n",[Cookie]),
      		      case ecas_db:get_ticket(Cookie) of
                           false->{render,"ecas/login.html",[{service,Url},{message,[]}],[]};
                               _->
                               if
        			    ServiceUrl=:=undefined ->
	 			        {render,"ecas/main.html",[],[]};
		       		    true ->
			 	       Url2=ServiceUrl++"?"++"ticket="++Cookie,
				        {redirect,Url2}
                                end
                       end
        end,
        io:format("~p~n",[Rtn]),
        Rtn;

handle_request("serviceValidate",[])->

    Stgt  = uri_params_middleware:get_param("ticket",Env),
    io:format("~p~n",[Stgt]),
    {_,_,UserName}=ecas_db:get_ticket(Stgt),
    io:format("~p~n",[UserName]),    
    {render,"ecas/validate.html",[{name,UserName}],[]};


handle_request("submit",[]) ->
    PostData = ewgi_api:remote_user_data(Env),
    io:format("~p~n",[PostData]),
    Un = proplists:get_value("username",PostData),
    Pw = proplists:get_value("password",PostData),
    ServiceUrl=proplists:get_value("service",PostData),
    R = case ecas_db:validate(Un,Pw) of
         true  ->
                %% cookie tst,uuid tgt,save ticket,redirct to user service
               %% Uuid = uuid:to_string(uuid:v4()),
                Uuid=uuid:get_uuid(),
		io:format("uuid:~p~n",[Uuid]),
                ecas_db:create_ticket(Uuid,Un),
                CasCookie = beepbeep_cookies:cookie("CASTGT", Uuid, [{path, "/"}]),
   	        if
		    ServiceUrl=:="" ->
			{render,"ecas/main.html",[],[]};
                    true ->
                 Url=ServiceUrl++"?"++"ticket="++Uuid,
                  CasCookieHeader=[{headers,[CasCookie]}],
    		{redirect,Url,CasCookieHeader}
               end ; 
              
        false ->
                Message=["user name or password error!"],
                {render,"ecas/login.html",[{service,""},{message,Message}],[]}
    end ,
    R .

%% If necessary, add these hooks:
%% *DON'T FORGET TO EXPORT THEM AS NECESSARY*

%% before_filter/0
%%
%% Should return one of:
%% ok
%% {render, View, Data}
%% {render, View, Data, Options}
%% {static, File}
%% {text, Data}
%% {redirect, Url}
%% {controller, ControllerName}
%% {controller, ControllerName, ActionName}
%% {controller, ControllerName, ActionName, Args}
%%
before_filter() ->
    FilterOnly = ["show"],
    case lists:member(beepbeep_args:get_action(Env),FilterOnly) of
	true ->
	    error_logger:info_report("Doing the filter for SHOW~n"),
	    ok;
	false ->
	    ok
    end.

%% before_render/1
%%
%% This hook accepts one of these tuples:
%% {render, View, Data}
%% {render, View, Data, Options}
%% {static, File}
%% {text, Data}
%%
%% Should return one of:
%% {render, View, Data}
%% {render, View, Data, Options}
%% {static, File}
%% {text, Data}
%% {redirect, Url}
%%
%% before_render({render, View, Data, Options}) ->
%%	{render, View, Data, Options}.
